home *** CD-ROM | disk | FTP | other *** search
Java Source | 2003-04-29 | 7.0 KB | 262 lines |
- package com.jproxy.samples.ejb.test;
- /*
- usage:
- java -cp samples.jar;proxyclient.jar:j2api.jar [-Djava.properties]
- com.jproxy.samples.ejb.test.PerformanceClient
- [ [http://|https://]YourServerName:[port] [#_of_loops] [#_of_bytes] [#_of_threads] ]
-
- where:
- #_of_loops - number of loops. Default 1"+
- #_of_bytes - number of bytes send(received) to host in request. Default 0"+
- #_of_threads - number of threads. Default 1"+
-
- usage example: Performance test WITH JProxy
- 10 loops, 10000 bytes, 100 threads (10*100 calls, 10*100*10000 sent bytes, 10*100*10000 received)
- %JAVA_HOME%\bin\java -cp samples.jar;proxyclient.jar;j2api.jar \
- -Djava.naming.factory.initial=com.jproxy.proxy.NamingContextFactory \
- -Djava.naming.provider.url=localhost:8080 \
- com.jproxy.samples.ejb.test.PerformanceClient \
- 10 10000 100
-
- usage example: Performance test WITHOUT JProxy (JBoss)
- 10 loops, 10000 bytes, 100 threads (10*100 calls, 10*100*10000 sent bytes, 10*100*10000 received)
- %JAVA_HOME%\bin\java -cp samples.jar; \
- %LIB_PATH%/jboss-j2ee.jar;%LIB_PATH%/jboss-client.jar;\
- %LIB_PATH%/jnp-client.jar;%LIB_PATH%/jbosssx-client.jar;\
- %LIB_PATH%/jboss-common-client.jar;%LIB_PATH%/log4j.jar \
- -Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory \
- -Djava.naming.provider.url=localhost \
- com.jproxy.samples.ejb.test.PerformanceClient \
- 10 10000 100
- */
- import javax.naming.*;
- import java.applet.*;
- import java.util.*;
- import java.io.*;
-
- import com.jproxy.proxy.*;
- import com.jproxy.samples.interfaces.ITest;
-
- public class PerformanceClient
- extends SessionClient
- implements Runnable
- {
- static String sync = "";
-
- int bytes = 0;
- int loops = 1;
- int loop = 0;
- int index = 0;
- float delay = 0;
- float minDelay = 0;
- float maxDelay = 0;
- byte[] buf = null;
- String log = "";
- Thread thread = new Thread(this);
- String error = null;
-
- public PerformanceClient()
- {
- }
-
-
- void fillBuffer()
- {
- /* to test remote exceptions thrown by EJB */
- if(bytes<0)
- {
- buf = null;
- return;
- }
- buf = new byte[bytes];
- for(int i=0; i<bytes; i++)
- buf[i] = (byte)(i & 0xFF);
- }
-
- public void test()
- throws Exception
- {
- fillBuffer();
-
- long t = System.currentTimeMillis();
-
- for(int i=0; i<loops; i++)
- {
- long tt = System.currentTimeMillis();
- session.echoBytes(buf);
- float d = (float)((System.currentTimeMillis()-tt)/1000.0);
- if(d<minDelay)
- minDelay = d;
- else if(d>maxDelay)
- maxDelay = d;
- loop++;
- }
- delay = (float)((System.currentTimeMillis()-t)/1000.0);
- log +=
- "\r\n*****************************************************"+
- "\r\nURL: "+env.getProperty(Context.PROVIDER_URL)+
- "\r\nNumber of loops: "+loops+
- "\r\nNumber of sent and received bytes per loop: "+bytes+
- "\r\nThread number: "+index+
- "\r\nMin time for loop: "+minDelay+" seconds"+
- "\r\nMax time for loop: "+maxDelay+" seconds"+
- "\r\nAverage time for loop: "+delay/loops+" seconds"+
- "\r\nTotal time for All loops: "+delay+" seconds"+
- "\r\n*****************************************************";
- }
-
- static void println(String msg)
- {
- synchronized(sync)
- {
- System.out.println(msg);
- }
- }
-
- public void run()
- {
- try{
- init();
- test();
- destroy();
- println(log);
- }
- catch(Exception e){
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- PrintWriter writer = new PrintWriter(baos);
- e.printStackTrace(writer);
- writer.flush();
- error = new String(baos.toByteArray());
- println("\r\n########### ERROR in thread "+index+"###########\r\n"+error);
- System.exit(1);
- }
- }
-
-
- public static void main(String[] arguments)
- {
- int threads = 1;
- String hostUrl = null;
- int loops = 1;
- int bytes = 0;
-
- Vector vargs = new Vector();
- for(int i=0; i<arguments.length; i++)
- if(arguments[i].startsWith("-D"))
- {
- int p = arguments[i].indexOf("=");
- System.setProperty(arguments[i].substring(2, p), arguments[i].substring(p+1));
- System.out.println("key = "+arguments[i].substring(2, p)+" val = "+arguments[i].substring(p+1));
- }
- else
- vargs.add(arguments[i]);
- String args[] = (String[])vargs.toArray(new String[]{});
-
- if(args.length==0)
- {
- System.out.println("usage: \r\njava -cp samples.jar;proxyclient.jar [-Djava.properties] "+
- "com.jproxy.samples.ejb.test.PerformanceClient "+
- "[#_of_loops] [#_of_bytes] [#_of_threads]"+
- "\r\n#_of_loops - number of loops. Default 1"+
- "\r\n#_of_bytes - number of bytes send(received) to host in request. Default 0"+
- "\r\n#_of_threads - number of threads. Default 1"+
- "\r\nWill try \"localhost\" ...");
- }
- if(args.length>0)
- {
- try{
- loops = Integer.parseInt(args[0]);
- if(loops<=0)
- throw new Exception();
- }catch(Exception e){
- System.out.println("Wrong #_of_loops parameter!");
- System.exit(1);
- }
- }
- if(args.length>1)
- {
- try{
- bytes = Integer.parseInt(args[1]);
- if(bytes<0)
- throw new Exception();
- }catch(Exception e){
- System.out.println("Wrong #_of_bytes parameter!");
- System.exit(1);
- }
- }
- if(args.length>2)
- {
- try{
- threads = Integer.parseInt(args[2]);
- if(threads<=0 | threads>1024)
- throw new Exception();
- }catch(Exception e){
- System.out.println("Wrong #_of_threads!");
- System.exit(1);
- }
- }
-
-
- long time = System.currentTimeMillis();
- PerformanceClient[] clients = new PerformanceClient[threads];
- for(int t=0; t<threads; t++)
- {
- clients[t] = new PerformanceClient();
-
- clients[t].env = System.getProperties();
- clients[t].loops = loops;
- clients[t].bytes = bytes;
- clients[t].index = t;
-
- clients[t].thread.start();
- }
- try{
- for(int t=0; t<threads; t++)
- if(clients[t].thread.isAlive())
- clients[t].thread.join();
- }
- catch(InterruptedException e){
- e.printStackTrace();
- }
- float totalTime = (float)((System.currentTimeMillis()-time)/1000.0);
-
- int errorIndex = -1;
- for(int t=0; t<threads; t++)
- if(clients[t]!=null && clients[t].error!=null)
- {
- errorIndex = t;
- break;
- }
- if(errorIndex==-1)
- {
- float minDelay = 0;
- float maxDelay = 0;
- float averageDelay = 0;
-
- for(int t=0; t<threads; t++)
- {
- if(minDelay>clients[t].minDelay)
- minDelay = clients[t].minDelay;
- else if(maxDelay<clients[t].maxDelay)
- maxDelay = clients[t].maxDelay;
- averageDelay += clients[t].delay/loops;
- }
- averageDelay /= threads;
- String msg = "\r\nTest executed seccessfully. No errors."+
- "\r\n*****************************************************"+
- "\r\nURL: "+clients[0].hostUrl+
- "\r\nNumber of loops: "+loops+
- "\r\nNumber of sent and received bytes per loop: "+bytes+
- "\r\nTotal number of Threads: "+threads+
- "\r\nTotal Number of sent and received bytes: "+bytes*threads*loops+
- "\r\nMin request time: "+minDelay+" seconds"+
- "\r\nMax request time: "+maxDelay+" seconds"+
- "\r\nAverage request time: "+averageDelay+" seconds"+
- "\r\nTotal time: "+totalTime+" seconds"+
- "\r\n*****************************************************";
- println(msg);
- }
- else
- println("Error during test execution!\r\n"+clients[errorIndex].error);
- }
- }